package org.dromara.basic.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.basic.domain.bo.MemberBo;
import org.dromara.basic.domain.vo.MemberVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.basic.domain.vo.PlaceVo;
import org.dromara.basic.domain.bo.PlaceBo;
import org.dromara.basic.service.IPlaceService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 跑区设置
 *
 * @author chl
 * @date 2024-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/basic/place")
public class PlaceController extends BaseController {

    private final IPlaceService placeService;

    /**
     * 查询跑区设置列表
     */
    @SaCheckPermission("basic:place:list")
    @GetMapping("/list")
    public TableDataInfo<PlaceVo> list(PlaceBo bo, PageQuery pageQuery) {
        return placeService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询跑区列表详细信息
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("basic:place:query")
    @GetMapping("/infoList")
    public R<List<PlaceVo>> getInfoList(PlaceBo bo) {
        return R.ok(placeService.queryList(bo));
    }
    /**
     * 导出跑区设置列表
     */
    @SaCheckPermission("basic:place:export")
    @Log(title = "跑区设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PlaceBo bo, HttpServletResponse response) {
        List<PlaceVo> list = placeService.queryList(bo);
        ExcelUtil.exportExcel(list, "跑区设置", PlaceVo.class, response);
    }

    /**
     * 获取跑区设置详细信息
     *
     * @param placeId 主键
     */
    @SaCheckPermission("basic:place:query")
    @GetMapping("/{placeId}")
    public R<PlaceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long placeId) {
        return R.ok(placeService.queryById(placeId));
    }

    /**
     * 新增跑区设置
     */
    @SaCheckPermission("basic:place:add")
    @Log(title = "跑区设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PlaceBo bo) {
        return toAjax(placeService.insertByBo(bo));
    }

    /**
     * 修改跑区设置
     */
    @SaCheckPermission("basic:place:edit")
    @Log(title = "跑区设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PlaceBo bo) {
        return toAjax(placeService.updateByBo(bo));
    }

    /**
     * 删除跑区设置
     *
     * @param placeIds 主键串
     */
    @SaCheckPermission("basic:place:remove")
    @Log(title = "跑区设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{placeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] placeIds) {
        return toAjax(placeService.deleteWithValidByIds(List.of(placeIds), true));
    }
}
